package com.icecola.d7_thread_communication;

import java.util.ArrayList;
import java.util.List;

public class Desk {
    private final List<String> list = new ArrayList<>();

    public synchronized void put() {
        try {
            String name = Thread.currentThread().getName();
            if (list.size() == 0) {
                list.add(name + "做了一个肉包子");
                System.out.println(name + "做了一个肉包子");
                Thread.sleep(1000);
            }
            this.notifyAll();
            this.wait();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void get() {
        try {
            String name = Thread.currentThread().getName();
            if (list.size() == 1) {
                //吃包子
                System.out.println(name + "吃包子");
                list.clear();
                Thread.sleep(1000);
            }
            this.notifyAll();
            this.wait();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
